package com.ltv.xiangqiplugin.models;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;

public class Messages extends Properties {
	private Vector<MessagesObserver> observers;
	private Languages lang;
	private BoardSizeEnum boardSize;
	private Themes theme;
	
	private static Messages instance;
	
	private static final String PROPERTIES_FILE_NAME = ".CCMaker";
	private static String propertiesFileName;
	private Properties preferences = new Properties();
	
	public static final String USER_LANGUAGE_KEY = "ccmaker.language";
	public static final String USER_BOARDSIZE_KEY = "ccmaker.boardsize";
	public static final String USER_THEME_KEY = "ccmaker.theme";
	
	private Messages() {
		super();
		observers = new Vector<MessagesObserver>();
		
		try {
			propertiesFileName = System.getProperty("user.home") +
		               System.getProperty("file.separator") +
		               PROPERTIES_FILE_NAME;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			preferences.load(new FileInputStream(propertiesFileName));
		} catch (Exception e) {
			preferences.setProperty(USER_LANGUAGE_KEY, Languages.EN.toString());
			preferences.setProperty(USER_BOARDSIZE_KEY, BoardSizeEnum.MEDIUM.toString());
		}
		
		try {
			if (Languages.EN.toString().equalsIgnoreCase(preferences.getProperty(USER_LANGUAGE_KEY))) {
				setLang(Languages.EN);
			} else if (Languages.VI.toString().equalsIgnoreCase(preferences.getProperty(USER_LANGUAGE_KEY))) {
				setLang(Languages.VI);
			} else {
				setLang(Languages.EN);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (BoardSizeEnum.SMALL.toString().equalsIgnoreCase(preferences.getProperty(USER_BOARDSIZE_KEY))) {
			setBoardSize(BoardSizeEnum.SMALL);
		} else if (BoardSizeEnum.MEDIUM.toString().equalsIgnoreCase(preferences.getProperty(USER_BOARDSIZE_KEY))) {
			setBoardSize(BoardSizeEnum.MEDIUM);
		} else if (BoardSizeEnum.LARGE.toString().equalsIgnoreCase(preferences.getProperty(USER_BOARDSIZE_KEY))) {
			setBoardSize(BoardSizeEnum.LARGE);
		} else {
			setBoardSize(BoardSizeEnum.MEDIUM);
		}
		
		if (Integer.toString(Themes.THEME_1.getValue()).equalsIgnoreCase(preferences.getProperty(USER_THEME_KEY))) {
			setTheme(Themes.THEME_1);
		} else if (Integer.toString(Themes.THEME_2.getValue()).equalsIgnoreCase(preferences.getProperty(USER_THEME_KEY))) {
			setTheme(Themes.THEME_2);
		} else {
			setTheme(Themes.THEME_1);
		}
	}
	
	public void addObserver(MessagesObserver observer) {
		observers.add(observer);
	}
	
	public void removeObserver(MessagesObserver observer) {
		observers.remove(observer);
	}
	
	public void notifyObservers() {
		for (MessagesObserver observer : observers) {
			observer.messagesChanged();
		}
	}

	public Languages getLang() {
		return lang;
	}

	public void setLang(Languages lang) throws Exception {
		if (this.lang != lang) {
			preferences.setProperty(USER_LANGUAGE_KEY, lang.toString());
			this.lang = lang;
			InputStream is = null;
	        InputStreamReader isr = null;
	        try {
	        	if (lang == Languages.EN) {
	        		is = getClass().getResourceAsStream("/messages/messages_en.txt");
	        	} else if (lang == Languages.VI) {
	        		is = getClass().getResourceAsStream("/messages/messages_vi.txt");
	        	}
	            isr = new InputStreamReader(is, "utf-8");
	            load(isr);
	        } finally {
	            if (is != null) {
	                is.close();
	            }
	            if (isr != null) {
	                isr.close();
	            }
	        }
		}
	}
	
	public BoardSizeEnum getBoardSize() {
		return boardSize;
	}

	public void setBoardSize(BoardSizeEnum boardSize) {
		this.boardSize = boardSize;
		preferences.setProperty(USER_BOARDSIZE_KEY, boardSize.toString());
	}
	
	public Themes getTheme() {
		return theme;
	}

	public void setTheme(Themes theme) {
		this.theme = theme;
		preferences.setProperty(USER_THEME_KEY, Integer.toString(theme.getValue()));
	}

	public static Messages getInstance() {
		if (instance == null) {
			instance = new Messages();
		}
		return instance;
	}
	
	public boolean storePreferences() {
		try {
			preferences.store(new FileOutputStream(propertiesFileName), "CCMaker User Preferences");
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
}
